package com.example.mobile2021606128wje;

import android.app.DownloadManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView textView2;
    private Button btnMain6days;
    private Button btnMainLimit;
    private Button btnMainAqi;
    private Button btnMainAlert;
    private Button btnMainCondition;
    private TextView tvMainDAta;
    OkHttpClient okHttpClient=new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
        btnMainCondition.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread( new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody=new FormBody.Builder()
                                .add( "cityId","917" )
                                .add( "token",MojiDataManager.CONDITIONTOKEN )
                                .build();
                        Request request=new Request.Builder( )
                                .addHeader( "Authorization","APPCODE"+MojiDataManager.AppCode )
                                .url( MojiDataManager.CONDITIONURL )
                                .post( requestBody )
                                .build();
                        Call call=okHttpClient.newCall( request );
                        call.enqueue( new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                runOnUiThread( new Runnable() {
                                    @Override
                                    public void run() {
                                        tvMainDAta.setText( result );
                                        Log.i( "TAG", "run: "+result );

                                    }
                                } );

                            }
                        } );
                    }
                } ).start();
            }
        } );
    }

    private void initView() {
        textView2 = (TextView) findViewById( R.id.textView2 );
        btnMain6days = (Button) findViewById( R.id.btnMain6days );
        btnMainLimit = (Button) findViewById( R.id.btnMainLimit );
        btnMainAqi = (Button) findViewById( R.id.btnMainAqi );
        btnMainAlert = (Button) findViewById( R.id.btnMainAlert );
        btnMainCondition = (Button) findViewById( R.id.btnMainCondition );
        tvMainDAta = (TextView) findViewById( R.id.tvMainDAta );
    }
}